<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h4>阶乘计算器：数据a内的所有数据阶乘</h4>
    <input type="text" id="num" placeholder="输入数据a" />
    <button id="btn">计算</button>
    <script>
      function calc(a) {
        var sum = 0;
        var tmp = 1;
        for (var i = 1; i <= a; ++i) {
          tmp *= i;
          //tmp为当前数字i的阶乘的值
          sum += tmp;
        }
        document.write(sum);
      }

      btn.onclick = function () {
        var a = parseInt(num.value);
        calc(a);
      };
    </script>
  </body>
</html>
